<template>
  <div class="rPwdBox">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="oldPassword"
        label="原始密码"
        placeholder="原始密码"
        :rules="[{ required: true, message: '旧密码' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="newPassword"
        label="新密码"
        placeholder="新密码"
        :rules="[
          {
            pattern: /^[0-9a-z]{4,12}$/i,
            message: '密码数字字母组成长度4到12位',
          },
        ]"
      />
      <van-field
        v-model="rePassword"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="[
          { validator: checkPassword, message: '两次输入的密码保持一致' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { resetPwd } from "@/service/mineService";
export default {
  data() {
    return {
      username: "",
      password: "",
      rePassword: "",
    };
  },
  methods: {
    checkPassword(val) {
      return val === this.password;
    },
    async onSubmit(values) {
      const info = {
        newPassword: values.newPassword,
        oldPassword: values.oldPassword,
      };
      const { data } = await resetPwd(info);
      if (data.code !== 200) return this.$toast(data.msg);
      this.$toast("修改成功");
      this.$emit("changeRePwdShow");
    },
  },
};
</script>

<style lang="scss" scoped>
.rPwdBox {
  width: 300px;
  height: 300px;
  background: #fff;
  color: #333;
  .van-form {
    margin-top: 80px;
  }
}
</style>
